import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';

import '../exception/AppException.dart';

/// 错误处理拦截器
class YZErrorInterceptor extends Interceptor {
  // 是否有网
  Future<bool> isConnected() async {
    // var connectivityResult = await (Connectivity().checkConnectivity());
    // return connectivityResult != ConnectivityResult.none;
    return true;
  }

  @override
  Future<void> onError(DioException err, ErrorInterceptorHandler errCb) async {
    // dio默认的错误实例，如果是没有网络，只能得到一个未知错误，无法精准的得知是否是无网络的情况
    if (err.type == DioExceptionType.unknown) {
      bool isConnectNetWork = await isConnected();
      if (!isConnectNetWork) {
        // err.message = "当前网络不可用，请检查您的网络";
      }
    }
    // error统一处理
    AppException appException = AppException.create(err);
    // 错误提示
    debugPrint('DioError===: ${appException.toString()}');
    // err.error = appException;
    return super.onError(err, errCb);
  }
}
